package com.ruyuan.seckilling.render.controller;

import com.ruyuan.seckilling.common.MapResult;
import com.ruyuan.seckilling.render.entity.PageConfig;
import com.ruyuan.seckilling.render.entity.PageTemplate;
import com.ruyuan.seckilling.render.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 测试使用
 */
@Slf4j
@RestController
@RequestMapping("/pageRender/test")
public class TestController {

   @Autowired
   private FileService fileService;

   @Autowired
   private PageTemplateController pageTemplateController;

   @Autowired
   private PageConfigController pageConfigController;

    // curl http://localhost:8001/test/step1
    @GetMapping("/step1")
    public void test1() throws IOException {

        // 1. 上传模板文件
        ClassPathResource activityFtl = new ClassPathResource("seckilling-activity.ftl");
        ClassPathResource productFtl = new ClassPathResource("seckilling-product.ftl");
        String activityFtlContent = IOUtils.toString(activityFtl.getInputStream(), StandardCharsets.UTF_8);
        String productFtlContent = IOUtils.toString(productFtl.getInputStream(), StandardCharsets.UTF_8);
        String activityFtlUrl = fileService.upload(activityFtlContent);
        String productFtlUrl = fileService.upload(productFtlContent);

        // 2. 添加模板
        PageTemplate pageTemplate = PageTemplate.builder()
                .templateName("秒杀活动商品列表模板")
                .templateUrl(activityFtlUrl)
                .build();
        MapResult result = pageTemplateController.save(pageTemplate);
        log.info("添加商品列表模板, id={}", result.get("pageTemplateId"));

        PageTemplate pageTemplate2 = PageTemplate.builder()
                .templateName("秒杀活动商品模板")
                .templateUrl(productFtlUrl)
                .build();
        MapResult result2 = pageTemplateController.save(pageTemplate2);
        log.info("添加商品模板, id={}", result2.get("pageTemplateId"));

        // 3. 添加页面配置
        PageConfig pageConfig = PageConfig.builder()
                .templateId(Long.parseLong(String.valueOf(result.get("pageTemplateId"))))
                .templateUrl(activityFtlUrl)
                .pageName("秒杀活动商品列表页面")
                .pageCode("seckilling_activity")
                .aggrDataUrl("http://ruyuan-seckilling-page-aggr-service/seckilling/activity")
                .build();
        MapResult result3 = pageConfigController.save(pageConfig);
        log.info("添加商品列表页面, id={}", result3.get("pageConfigId"));

        PageConfig pageConfig2 = PageConfig.builder()
                .templateId(Long.parseLong(String.valueOf(result2.get("pageTemplateId"))))
                .templateUrl(productFtlUrl)
                .pageName("秒杀活动商品页面")
                .pageCode("seckilling_product")
                .aggrDataUrl("http://ruyuan-seckilling-page-aggr-service/seckilling/product")
                .build();
        MapResult result4 = pageConfigController.save(pageConfig2);
        log.info("秒杀活动商品页面, id={}", result4.get("pageConfigId"));
    }
}
